var app = getApp();
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
  data: {
    isWxapp: true,
    isLogin: false,
    title:'请先绑定账号',
    userInfo: {
      id: 0,
      avatarUrl: '/assets/images/avatar.png',
      nickName: '未登录',
      balance: 0,
      score: 0,
      level: 0,
    }
  },
  onLoad: function () {
    qqmapsdk = new QQMapWX({
      key: 'S3OBZ-YGPW2-VSFUH-CGEKB-RUVPS-PMFKF'
  });

  },
  onShow: function () {
    var that = this;
    if (wx.getStorageSync('userInfo')) {
      that.setData({ userInfo: wx.getStorageSync('userInfo'), isWxapp: that.isWxapp(), isLogin:true });
      that.getopenid()
    }
    
  },
  getopenid() {

    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(wx.getStorageSync('userInfo'))
        wx.request({
          url: app.apiUrl + '/api/index/getopenid',
          data: {
            code: res.code,
            avatarUrl:wx.getStorageSync('userInfo').avatarUrl,
            nickname:wx.getStorageSync('userInfo').nickName
          },
          method: 'POST',
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          success: res=> {
           console.log(res.data.openid)
            wx.setStorageSync('openid',res.data.openid)
            this.checktitle()
          }
        })
      }
    })
},
  login: function () {
    var that = this;
    wx.getUserProfile({
      lang:'zh',
      desc:'授权用户信息',
      success:function(res){
        wx.setStorageSync('userInfo',res.userInfo)
        that.setData({ userInfo: wx.getStorageSync('userInfo'), isWxapp: that.isWxapp(), isLogin:true });
        that.getopenid()
      },
      fail:function(e){
        app.info(JSON.stringify(e));
      }
    })
  },
  logout: function () {
    var that = this;
    wx.removeStorageSync('userInfo')
    wx.removeStorageSync('openid')

      that.setData({ userInfo: {
          id: 0,
          avatar: '/assets/images/avatar.png',
          nickname: '游客',
          balance: 0,
          score: 0,
          level: 0
        }, isWxapp: that.isWxapp(), isLogin: false,title:'请先绑定账号' });
   
  },
  isWxapp: function () {
    return app.globalData.userInfo ? app.globalData.userInfo.username.match(/^u\d+$/) : true;
  },

  checktitle: function () {
 
    var that = this;
    wx.request({
      url: app.apiUrl + '/api/index/checktitle',
      data: {
        openid:wx.getStorageSync('openid'),
      },
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success: function (res) {
        if(res.data){
          that.setData({ title: res.data});
          wx.setStorageSync('title',res.data)
        }else{
          that.setData({ title:'您暂未绑定账号或绑定的账号暂时无权限,您可以完善个人资料中的信息系统会自动绑定您的账号'});
        }
     
      },
      error: function (res) {
        that.setData({ title:'您暂未绑定账号或绑定的账号暂时无权限,您可以完善个人资料中的信息系统会自动绑定您的账号'});
      }
    });

  
  },

  baibandaka: function () {
    if (!wx.getStorageSync('openid')) {
      app.error("请登录后再操作");
      return false;
    }

    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
         var locationString = res.latitude + "," + res.longitude;
          var latitude = res.latitude
          var longitude = res.longitude
          qqmapsdk.reverseGeocoder({//SDK调用
            location: { latitude, longitude },
            success: function (res) {
              var dizhi = res.result.address
              wx.request({
                url: app.apiUrl + '/api/index/baibandaka',
                data: {
                  openid: wx.getStorageSync('openid'),
                  dizhi:dizhi
                },
                method: 'POST',
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                success: res=> {
                
                  wx.showModal({
                    title: '温馨提示',
                    content: res.data,
                    showCancel: false
                  });

                  
                }
              })
            }
          })
      },
  });
  },


  wanbandaka: function () {
    if (!wx.getStorageSync('openid')) {
      app.error("请登录后再操作");
      return false;
    }

    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
         var locationString = res.latitude + "," + res.longitude;
          var latitude = res.latitude
          var longitude = res.longitude
          qqmapsdk.reverseGeocoder({//SDK调用
            location: { latitude, longitude },
            success: function (res) {
              var dizhi = res.result.address
              wx.request({
                url: app.apiUrl + '/api/index/wanbandaka',
                data: {
                  openid: wx.getStorageSync('openid'),
                  dizhi:dizhi
                },
                method: 'POST',
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                success: res=> {
                
                  wx.showModal({
                    title: '温馨提示',
                    content: res.data,
                    showCancel: false
                  });

                  
                }
              })
            }
          })
      },
  });

  },


  bbuqian: function () {
    if (!wx.getStorageSync('openid')) {
      app.error("请登录后再操作");
      return false;
    }

    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
         var locationString = res.latitude + "," + res.longitude;
          var latitude = res.latitude
          var longitude = res.longitude
          qqmapsdk.reverseGeocoder({//SDK调用
            location: { latitude, longitude },
            success: function (res) {
              var dizhi = res.result.address
              wx.request({
                url: app.apiUrl + '/api/index/baibanbuqian',
                data: {
                  openid: wx.getStorageSync('openid'),
                  dizhi:dizhi
                },
                method: 'POST',
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                success: res=> {
                
                  wx.showModal({
                    title: '温馨提示',
                    content: res.data,
                    showCancel: false
                  });

                  
                }
              })
            }
          })
      },
  });

  },



  wbuqian: function () {
    if (!wx.getStorageSync('openid')) {
      app.error("请登录后再操作");
      return false;
    }

    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
         var locationString = res.latitude + "," + res.longitude;
          var latitude = res.latitude
          var longitude = res.longitude
          qqmapsdk.reverseGeocoder({//SDK调用
            location: { latitude, longitude },
            success: function (res) {
              var dizhi = res.result.address
              wx.request({
                url: app.apiUrl + '/api/index/wanbanbuqian',
                data: {
                  openid: wx.getStorageSync('openid'),
                  dizhi:dizhi
                },
                method: 'POST',
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                success: res=> {
                
                  wx.showModal({
                    title: '温馨提示',
                    content: res.data,
                    showCancel: false
                  });

                  
                }
              })
            }
          })
      },
  });

  },



  // dakanew: function () {
  //   if (!wx.getStorageSync('openid')) {
  //     app.error("请登录后再操作");
  //     return false;
  //   }

  //   wx.getLocation({
  //     type: 'gcj02',
  //     success: (res) => {
  //        var locationString = res.latitude + "," + res.longitude;
  
  //         var latitude = res.latitude
  //         var longitude = res.longitude

    
  //         qqmapsdk.reverseGeocoder({//SDK调用

  //           location: { latitude, longitude },
  //           success: function (res) {
  //             var dizhi = res.result.address
          
  //             wx.request({
  //               url: app.apiUrl + '/api/index/dakanew',
  //               data: {
  //                 openid: wx.getStorageSync('openid'),
  //                 dizhi:dizhi
  //               },
  //               method: 'POST',
  //               header: { 'content-type': 'application/x-www-form-urlencoded' },
  //               success: res=> {
  //                   if(res.data == 1){
  //                     wx.showModal({
  //                       title: '温馨提示',
  //                       content: '签到成功',
  //                       showCancel: false
  //                     });
  //                   }else{
  //                     wx.showModal({
  //                       title: '温馨提示',
  //                       content: '签到失败',
  //                       showCancel: false
  //                     });
  //                   }
                  
  //               }
  //             })
  //           }
  //         })
  //     },
  // });

  // },


  xiabandaka: function () {
    if (!wx.getStorageSync('openid')) {
      app.error("请登录后再操作");
      return false;
    }

    wx.getLocation({
      type: 'wgs84',
      altitude: false,
      success: (res) => {
         var locationString = res.latitude + "," + res.longitude;
          var latitude = res.latitude
          var longitude = res.longitude
          qqmapsdk.reverseGeocoder({//SDK调用
            location: { latitude, longitude },
            success: function (res) {
              console.log(res.result.address)
              var dizhi = res.result.address
              
    var content = '打卡失败'

    wx.request({
      url: app.apiUrl + '/api/index/xiabandaka',
      data: {
        openid: wx.getStorageSync('openid'),
        dizhi:dizhi
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
     
       wx.showModal({
        title: '温馨提示',
        content: res.data,
        showCancel: false
      });
      }
    })
            }
          })
      },
  });

  },

  buka: function () {
    if (!wx.getStorageSync('openid')) {
      app.error("请登录后再操作");
      return false;
    }


  },


  saoyisao: function () {

    if (!wx.getStorageSync('openid')) {
      app.error("请登录后再操作");
      return false;
    }
    wx.scanCode({
      success (res) {
        console.log(res)
      }
    })
  },
  showTips: function (event) {
    var tips = {
      balance: '余额',
      score: '积分',
      level: '等级',
    };
    var type = event.currentTarget.dataset.type;
    var content = tips[type];
    wx.showModal({
      title: '温馨提示',
      content: content,
      showCancel: false
    });
  },
  //点击头像上传
  uploadAvatar: function () {
    if (!wx.getStorageSync('userInfo')) {
      app.error("请登录后再操作");
      return false;
    }
    var that = this;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var formData = app.globalData.config.upload.multipart;
        formData.token = app.globalData.userInfo.token;
        wx.uploadFile({
          url: app.globalData.config.upload.uploadurl,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: formData,
          success: function (res) {
            if(res.statusCode != 200){
              app.error(res.errMsg);
              return;
            }
            var row = JSON.parse(res.data);
            if (row.code == 1) {
              app.request("/addons/cms/wxapp.user/avatar", { avatar: row.data.url }, function (data, ret) {
                app.success('头像上传成功!');
                app.globalData.userInfo = data.userInfo;
                that.setData({ userInfo: data.userInfo, isWxapp: that.isWxapp()});
              }, function (data, ret) {
                app.error(ret.msg);
              });
            }
          }, error: function (res) {
            app.error("上传头像失败!");
          }
        });
      }, error: function (res) {
        app.error("上传头像失败!");
      }
    });
  },
  goVip(e){
    if(!this.data.userInfo.is_install_vip){
      app.error('请安装VIP会员插件或启用此插件');
      return;
    }
    if(!this.isLogin){
      app.error('请登录后再操作');
      return;
    }
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  goPage(e){
      console.log(e.currentTarget.dataset.url)

    if(!wx.getStorageSync('userInfo')){
      app.error('请登录后再操作');
      return;
    }

    
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    });
  },
  checkLogin(e){
    return;
      console.log(e);
      return false;
  }
})
